<?php
/*
+--------------------------------------------------------------------------
|   phpBIZbiz v3.0 full version
|   ========================================
|   by taft@wjl.cn
|   http://www.phpbiz.cn
|   all rights reserved
+---------------------------------------------------------------------------
|
|   > 
|   > 最后修改日期：2005-9-9
|   > 
|
+--------------------------------------------------------------------------
*/
!function_exists('do_login') && exit('Forbidden');
$idx = new charge;
class charge
{
	function charge()
	{
		global $INCOME,$ADMIN;
		
		//---------------------------------------
		// Kill globals - globals bad, Homer good.
		//---------------------------------------
		
		$tmp_in = array_merge( $_GET, $_POST, $_COOKIE );
		
		foreach ( $tmp_in as $k => $v )
		{
			unset($$k);
		}
		switch($INCOME['code'])
		{
			case 'dosearch':
				$this -> do_search();
			    break;
			case 'free':
				$this -> free_form();
		    case 'dofree':
				$this -> do_free();
			case 'set':
				$this -> set();
			default:
				$this -> search_form();
			break;
		}
	
	}
	function search_form()
	{
		global $ADMIN,$SKIN;

		$ADMIN -> page_title = "查询充值记录";
		$ADMIN -> page_instruction = "您在这里可以按照不同的条件查询商家的充值情况。";
		$SKIN->td_header[] = array( ""  , "40%" );
		$SKIN->td_header[] = array( ""  , "60%" );
        $ADMIN->html .= $SKIN ->get_js_show();
		$ADMIN->html .= $SKIN->table_head( "" );
		$ADMIN->html .= $SKIN->form_head();
		$ADMIN->html .= $SKIN->form_input("code","dosearch","hidden");

		$ADMIN->html .= $SKIN->table_row( array( "<b>查询方式</b>" ,
										 " <input type='radio' name='t' id='green' value='0' onClick='showinfo(0);' checked>近期数据查询&nbsp;<input type='radio' name='t'  value='1' id='red' onClick='showinfo(1);'>历史数据查询" )
								    );
	    // caculate time stuff here
		$timestamp = time();
		$sec = 3600*24;
		for($i=0;$i<3;$i++)
		{
			$span = $timestamp-$sec*$i;
			$date[] = date("Y",$span).'-'.date("m",$span).'-'.date("d",$span);
		}
		list($d,$d2,$d3) = $date;

		for($i=2000;$i<2016;$i++)
			$year[] = array($i,"$i");
		for($i=1;$i<=12;$i++)
			$month[] = array($i,"$i");
		for($i=1;$i<=31;$i++)
			$day[] =  array($i,"$i");
		// end

		$ADMIN ->html .= $SKIN -> table_row( array("<b>选择查询时间</b>","<input name='tdate' value='$d' type='radio' checked>$d<input name='tdate' value='$d2' type='radio' >$d2<input name='tdate' value='$d3' type='radio' >$d3"),"","left","id='show1' style=''" );
	    $ADMIN ->html .= $SKIN -> table_row( array("<b>开始时间</b>",$SKIN->form_droplist("year",$year,date("Y",$timestamp))."年".$SKIN->form_droplist("month",$month,date("m",$timestamp)-1)."月".$SKIN->form_droplist("day",$day,date("d",$timestamp))."日"  ),"","left","id='show2' style='display:none;'" );
		$ADMIN ->html .= $SKIN -> table_row(	array("<b>结束时间</b>",$SKIN->form_droplist("year2",$year,date("Y",$timestamp))."年".$SKIN->form_droplist("month2",$month,date("m",$timestamp))."月".$SKIN->form_droplist("day2",$day,date("d",$timestamp))."日" ),"","left","id='show3' style='display:none;'" );
		$ADMIN ->html .= $SKIN -> table_row( array("设置每页显示记录条数：","<input type='text' value='20' name='pages' size='2'> 条/页") );
      
		$ADMIN->html .= $SKIN ->form_end();
		$ADMIN->html .= $SKIN ->table_end();
		$ADMIN->output();
	}
	function free_form()
	{
		global $ADMIN,$SKIN,$_LEVEL;

		if(count($_LEVEL[level])!=0)
		{
			foreach( $_LEVEL[level] as $k => $v )
			{
				$level[] = array( $v[0],$v[1]);
			}
		}

		$ADMIN -> page_title = "赠送点数";
		$ADMIN -> page_instruction = "您可以按照会员帐号或会员等级赠送点数。";
		$SKIN->td_header[] = array( ""  , "40%" );
		$SKIN->td_header[] = array( ""  , "60%" );
        $ADMIN->html .= $SKIN ->get_js_show();
		$ADMIN->html .= $SKIN->table_head( "" );
		$ADMIN->html .= $SKIN->form_head();
		$ADMIN->html .= $SKIN->form_input("code","dofree","hidden");
		$ADMIN->html .= $SKIN->table_row( array( "<b>赠送方式</b>" ,
										 " <input type='radio' name='t' id='green' value='1' onClick='showinfo(0);' checked>指定会员赠送&nbsp;<input type='radio' name='t'  value='2' id='red' onClick='showinfo(1);'>指定会员类别赠送" ));
		$ADMIN ->html .= $SKIN -> table_row( array("<b>输入会员名</b>","<input name='name' type='text' > <INPUT TYPE='checkbox' NAME='ex' value='1' checked>精确匹配"),"","left","id='show1' style=''" );
		$ADMIN ->html .= $SKIN -> table_row( array("<b>选择会员等级</b>",$SKIN->form_droplist("level_id",$level)),"","left","id='show2' style='display:none;'" );
		$ADMIN ->html .= $SKIN -> table_row( array("<b>赠送点数</b>",$SKIN->form_input("point")) );
		      
		$ADMIN->html .= $SKIN ->form_end();
		$ADMIN->html .= $SKIN ->table_end();
		$ADMIN->output();

	}
	function do_free()
	{
		global $DB,$SKIN,$ADMIN,$INCOME,$_LEVEL;
		$point = intval($INCOME[point]);
		if(!$point) $ADMIN->error("请输入大于0的点数");
		if( $INCOME[t]==2 )
		{
			$lid = $INCOME[level_id]; 
			if(!intval($INCOME[point])) $ADMIN->error("请输入大于0的点数");
			$DB -> db_query("UPDATE biz_seller_data SET money = money+".$point." WHERE property_id=".$INCOME[level_id] );
		    $ADMIN ->record_log("给等级为‘{$_LEVEL[$lid][1]}’的会员赠送{$point}点");
			$ADMIN->done_screen("赠送成功", "赠送点数", "act=charge&code=free" );

		}
		elseif($INCOME[t]==1)
		{	
			$name = $INCOME[name];
			$query = $INCOME[ex]==1?"WHERE `seller_logaccount` = '$name'":" WHERE `seller_logaccount` LIKE '%$name%'";
			$DB -> db_query( "SELECT `seller_logaccount`,`seller_id` FROM biz_seller ".$query);
			if(!$DB -> db_fetch_num()) $ADMIN->error("没有找到指定会员");
			while($r = $DB -> db_fetch_row() )
			{
				$list[] = array($r[seller_id],$r[seller_logaccount]);
			}
			$SKIN->td_header[] = array( ""  , "40%" );
			$SKIN->td_header[] = array( ""  , "60%" );
			$ADMIN->html .= $SKIN->table_head( "" );
			$ADMIN->html .= $SKIN->form_head();
			$ADMIN->html .= $SKIN->form_input("code","dofree","hidden");
			$ADMIN->html .= $SKIN->form_input("step","2","hidden");
			$ADMIN->html .= $SKIN->form_input("point",$point,"hidden");
			$ADMIN->html .= $SKIN->table_row( array("选择会员",$SKIN->form_droplist("seller_id",$list)) );
			$ADMIN->html .= $SKIN ->form_end();
			$ADMIN->html .= $SKIN ->table_end();
			$ADMIN->output();

		}elseif($INCOME[step]==2)
		{
			$DB -> db_query("UPDATE biz_seller_data SET money = money+$point WHERE seller_id =".$INCOME[seller_id]);
			$ADMIN ->record_log("给ID为‘{$INCOME[seller_id]}’的会员赠送{$point}点");
			$ADMIN->done_screen("赠送成功", "赠送点数", "act=charge&code=free" );
		}
	}

	function do_search()
	{
		global $INCOME,$DB,$SKIN,$ADMIN;
        //print_r($INCOME);
		if($INCOME[t]==0&&$INCOME[tdate])
		{
			$tdatedata = explode("-",$INCOME[tdate]);
			$from_time = mktime(0, 0, 0,$tdatedata[1],$tdatedata[2],$tdatedata[0]);
			$end_time = mktime(23,59,59,$tdatedata[1],$tdatedata[2],$tdatedata[0]);
		}elseif($INCOME[t]==1){
		    $from_time = mktime(0,0,0,$INCOME[month],$INCOME[day],$INCOME[year]);
			$end_time = mktime(23,59,59,$INCOME[month2],$INCOME[day2],$INCOME[year2]);
		}elseif($INCOME[from_time]&&$INCOME[end_time])
		{
            $from_time = $INCOME[from_time];$end_time = $INCOME[end_time];
		}
		$start = $INCOME['st'] ? intval( $INCOME['st'] ) : 0;
		$DB -> db_query("SELECT count(*) as count FROM biz_charge_log WHERE time>$from_time AND time<$end_time");
		$row_count = $DB -> db_fetch_row();
		$pages = $SKIN->build_pagelinks( array('TOTAL_POSS'  => $row_count['count'],
											   'PER_PAGE'    => intval($INCOME[pages]),
											   'CUR_ST_VAL'  => $start,
											   'L_SINGLE'    => "一页",
											   'L_MULTI'     => "页：",
											   'BASE_URL'    => $this->base_url."?act=charge"."&code={$INCOME['code']}&from_time={$from_time}&end_time={$end_time}&pages={$INCOME[pages]}"
											   )
											);
		$ADMIN->page_title = "本次记录搜索结果";

		$query = "SELECT * FROM biz_charge_log WHERE time>$from_time AND time<$end_time ORDER BY time DESC LIMIT  $start,$INCOME[pages]";
		
		$SKIN->td_header[] = array( "操作员"  , "15%" );
		$SKIN->td_header[] = array( "商家","10%");
		$SKIN->td_header[] = array( "数额" , "10%" );
		$SKIN->td_header[] = array( "时间" , "10%" );
	    
		$ADMIN->html .= $SKIN->table_head( "{$row_count['count']} "."条结果" );
		$DB -> db_query($query);
		while ( $r = $DB->db_fetch_row() )
		{
			$tm = $r['time']?date("Y-m-d H:i",$r['time']):'-';
			$ADMIN->html .= $SKIN->table_row( array("$r[operator]","$r[seller_logaccount]","$r[money]","$tm") );
		}
		$ADMIN->html .= $SKIN->td_basic($pages, 'right', 'pformstrip');
		$ADMIN->html .= $SKIN->table_end();
		$ADMIN->output();
	}
	function set()
	{
		global $ADMIN,$INCOME,$DB,$SKIN,$CHARGE_ITEM,$std;
        $cache_file= ROOT_PATH."cache/fee_cache.php";
		if(!$INCOME[step])
		{
            $DB -> db_query("SELECT * FROM biz_global_configure WHERE `key`='fee_db'"); 
			$r = $DB ->db_fetch_row();
            $feedb = unserialize($r['value']);
			$ADMIN -> page_title = "消费设定";
			$ADMIN -> page_instruction = "您在这里可以为phpBIZ设定各项消费点数。";
			$SKIN->td_header[] = array( ""  , "15%" );
			$SKIN->td_header[] = array( ""  , "" );
			$ADMIN -> html = $SKIN -> table_head();
			$ADMIN -> html .= $SKIN ->form_head();
			$ADMIN -> html .= $SKIN ->form_input("code","set","hidden");
			$ADMIN -> html .= $SKIN ->form_input("step","1","hidden");	
			foreach(  $CHARGE_ITEM as $k=>$v )
			{
				$ADMIN -> html .= $SKIN ->table_row( array($v,$SKIN->form_input("feedb[$k]",$feedb[$k])." 点" ) );
			}
			$ADMIN -> html .=$SKIN -> form_end();
			$ADMIN -> html .=$SKIN -> table_end();
            $ADMIN -> output();
		}else{
			$feedb = $INCOME[feedb];
			foreach( $feedb as $k=> $v)
				$feedb[$k]=intval($v);			
			$master['fee_db'] = serialize($feedb);
			$std -> rebuild_config($master);
			
			$level_cache = new cache;
			$level_cache -> cache_fee();

			$ADMIN ->record_log("更新消费设置");
			$ADMIN->done_screen("设置成功", "消费设置", "act=charge&code=free" );
		}
	}
}
?>